
package com.dxj1992.controller.admin.system;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dxj1992.controller.admin.BaseController;
import com.dxj1992.entity.AdminRolePermissions;
import com.dxj1992.entity.AdminRoles;
import com.dxj1992.entity.AdminUsers;
import com.dxj1992.service.IAdminRolePermissionsService;
import com.dxj1992.service.IAdminRolesService;
import com.dxj1992.service.IAdminUsersService;
import com.dxj1992.utils.ClearCache;
import com.dxj1992.utils.CommonResult;
import com.dxj1992.utils.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/admin/system/role")
@Slf4j
public class AdminRolesController extends BaseController {

    @Autowired
    private IAdminRolesService adminRolesService;
    @Autowired
    private IAdminRolePermissionsService adminRolePermissionsService;
    @Autowired
    private IAdminUsersService adminUsersService;
    @Autowired
    private ClearCache clearCache;

    @GetMapping("")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "admin/system/role/index";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Integer pageNum, Integer pageSize, AdminRoles adminRoles) {
        LambdaQueryWrapper<AdminRoles> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(adminRoles.getRoleName())) {
            queryWrapper.like(AdminRoles::getRoleName, adminRoles.getRoleName());
        }

        if (StrUtil.isNotBlank(adminRoles.getStatus())) {
            queryWrapper.eq(AdminRoles::getStatus, adminRoles.getStatus());
        }

        startPage(pageNum, pageSize);
        List<AdminRoles> list = adminRolesService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add() {
        return "admin/system/role/add";
    }

    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public CommonResult addSave(@Param("menuIds") String menuIds, AdminRoles adminRoles) {

        String[] split = menuIds.split(",");
        List<String> ids = Arrays.asList(split);

        if (ids.get(0) == null || ids.get(0) == "") {
            return CommonResult.failed("请选择权限");
        }

        boolean b = adminRolesService.save(adminRoles);
        clearCache.clearAdminCache();
        if (!b) {
            return CommonResult.success("添加失败");
        }

        List<AdminRolePermissions> list = new ArrayList<>();
        for (String id : ids) {
            AdminRolePermissions adminRolePermissions = new AdminRolePermissions();
            adminRolePermissions.setAdminRoleId(adminRoles.getId());
            adminRolePermissions.setAdminPermissionId(Integer.valueOf(id));
            list.add(adminRolePermissions);
        }

        adminRolePermissionsService.saveBatch(list, 50);

        return CommonResult.success("添加成功");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        AdminRoles adminRoles = adminRolesService.getOne(Wrappers.<AdminRoles>lambdaQuery().eq(AdminRoles::getId, id));
        mmap.put("role", adminRoles);
        return "admin/system/role/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    @Transactional
    public CommonResult editSave(AdminRoles adminRoles) {
        boolean b = adminRolesService.editSave(adminRoles);
        clearCache.clearAdminCache();
        if (!b) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @GetMapping("/authUser/{id}")
    public String authUser(@PathVariable("id") String id, ModelMap mmap) {
        AdminRoles adminRoles = adminRolesService.getOne(Wrappers.<AdminRoles>lambdaQuery().eq(AdminRoles::getId, id));
        mmap.put("role", adminRoles);
        return "admin/system/role/authUser";
    }

    @PostMapping("/authUser/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(Integer pageNum, Integer pageSize, AdminUsers adminUsers) {
        startPage(pageNum, pageSize);
        List<AdminUsers> list = adminUsersService.allocatedList(adminUsers);
        return getDataTable(list);
    }

    @GetMapping("/authUser/selectUser/{id}")
    public String selectUser(@PathVariable("id") String id, ModelMap mmap) {
        AdminRoles adminRoles = adminRolesService.getOne(Wrappers.<AdminRoles>lambdaQuery().eq(AdminRoles::getId, id));
        mmap.put("role", adminRoles);
        return "admin/system/role/selectUser";
    }

    @PostMapping("/authUser/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(Integer pageNum, Integer pageSize, AdminUsers adminUsers) {
        startPage(pageNum, pageSize);
        List<AdminUsers> list = adminUsersService.selectUnallocatedList(adminUsers);
        return getDataTable(list);
    }

    @PostMapping("/authUser/selectAll")
    @ResponseBody
    public CommonResult selectAll(@Param("userIds") String userIds, AdminUsers adminUsers) {
        Boolean b = adminUsersService.selectAll(userIds, adminUsers);
        clearCache.clearAdminCache();
        if (!b) {
            return CommonResult.failed("添加失败");
        }
        return CommonResult.success("添加成功");
    }

    @PostMapping("/authUser/cancelAll")
    @ResponseBody
    public CommonResult cancelAll(@Param("userIds") String userIds, AdminUsers adminUsers) {
        Boolean b = adminUsersService.cancelAll(userIds, adminUsers);
        clearCache.clearAdminCache();
        if (!b) {
            return CommonResult.failed("取消失败");
        }
        return CommonResult.success("取消成功");
    }

    @PostMapping("/remove")
    @ResponseBody
    public CommonResult remove(@Param("ids") String ids) {
        Boolean b = adminUsersService.removeData(ids);
        clearCache.clearAdminCache();
        if (!b) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }
}
